package cn.hznc.controller.toolfork;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.controller.service.toolfork.AssemblyToolService;
import cn.hznc.controller.toolfork.strategy.GetTransferToolByChildStrategy;
import cn.hznc.controller.toolfork.strategy.GetTransferToolContext;
import cn.hznc.domain.entity.nx.NxProgramEntity;
import cn.hznc.domain.entity.nx.NxSubprogramEntity;
import cn.hznc.domain.request.nx.NxProgramParamsReq;
import cn.hznc.domain.request.nx.NxSubprogram;
import cn.hznc.domain.request.toolfork.GetToolListReq;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.excel.importvo.ImportNxParamsTransferVo;
import cn.hznc.exception.ToolManageException;
import cn.hznc.mapper.NxProgramMapper;
import cn.hznc.mapper.NxSubprogramMapper;
import cn.hznc.utils.UUIDUtil;
import cn.hznc.vo.ParseNxParamsTransferVo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/10/12 16:42
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class OutputController{

    @Resource
    public AssemblyToolService assemblyToolService;

    @Resource
    public NxProgramMapper nxProgramMapper;

    @Resource
    public NxSubprogramMapper nxSubprogramMapper;

    @Resource
    public GetTransferToolByChildStrategy getTransferToolByChildStrategy;

    @RequestMapping(value = "/GetToolListWithPages", method = RequestMethod.POST)
    public ResponseBody GetToolListWithPages(@RequestBody GetToolListReq getToolListReq) {
        GetTransferToolContext context = new GetTransferToolContext(getTransferToolByChildStrategy);
        //return ResponseBody.success(assemblyToolService.GetToolListWithPages(getToolListReq));
        return ResponseBody.success(context.GetTransferTool(getToolListReq));
    }

    @RequestMapping(value = "/GetTool", method = RequestMethod.GET)
    public ResponseBody GetTool(@RequestParam("ID") String ID , @RequestParam("department") String department , @RequestParam("name") String name) {
        return ResponseBody.success(assemblyToolService.GetTool(ID ,department , name));
        //return ResponseBody.success(assemblyToolService.GetToolReload(ID ,department));
    }

    @RequestMapping(value = "/GetToolReload", method = RequestMethod.GET)
    public ResponseBody GetToolReload(@RequestParam("ID") String ID , @RequestParam("department") String department) {
        //return ResponseBody.success(assemblyToolService.GetTool(ID ,department));
        return ResponseBody.success(assemblyToolService.GetToolReload(ID ,department));
    }

    @RequestMapping(value = "/GetCutParams", method = RequestMethod.GET)
    public ResponseBody getToolCuttingParam(@RequestParam("ID") String ID , @RequestParam("department") String department , @Param("name") String name) {
        try {
            return ResponseBody.success(assemblyToolService.getToolCuttingParam(ID ,department , name));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode() , e.getMessage());
        }
    }

    @RequestMapping(value = "/getTransferParamCode", method = RequestMethod.GET)
    public ResponseBody getTransferParamCode() {
        return ResponseBody.success(assemblyToolService.getTransferParamCode());
    }

    @RequestMapping(value = "/checkAssemblyExist", method = RequestMethod.GET)
    public ResponseBody checkAssemblyExist(@RequestParam Integer assemblyToolId) {
        try {
            return ResponseBody.success(assemblyToolService.checkAssemblyExist(assemblyToolId));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode() , e.getMessage());
        } catch (IOException e) {
            return ResponseBody.error("1001" , "图片上传失败");
        }
    }

    @RequestMapping(value = "/RunAssemblyTask", method = RequestMethod.GET)
    public ResponseBody RunAssemblyTask(@RequestParam Integer assemblyToolId) {
        return ResponseBody.success(assemblyToolService.RunAssemblyTask(assemblyToolId));
    }

    @RequestMapping(value = "/GetAssemblyTaskStatus", method = RequestMethod.GET)
    public ResponseBody GetAssemblyTaskStatus(@RequestParam Integer assemblyToolId) {
        try {
            return ResponseBody.success(assemblyToolService.GetAssemblyTaskStatus(assemblyToolId));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode() , e.getMessage());
        } catch (IOException e) {
            return ResponseBody.error("1001" , "图片上传失败");
        }
    }

    @RequestMapping(value = "/importNxParamsTransferExcel", method = RequestMethod.POST)
    public ResponseBody importNxParamsTransferExcel(@RequestPart("file") MultipartFile file){
        try {
            List<ParseNxParamsTransferVo> list = new ArrayList<>();
            EasyExcel.read(file.getInputStream(), ImportNxParamsTransferVo.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    //获取当前的sheet，从0开始
                    //获取当前的行数，
                    Integer rowNum = analysisContext.readRowHolder().getRowIndex();//获取行数
                    Integer seIndex = analysisContext.readSheetHolder().getReadSheet().getSheetNo();
                    String sheetName = analysisContext.readSheetHolder().getReadSheet().getSheetName();
                    ImportNxParamsTransferVo data = (ImportNxParamsTransferVo) o;
                    if(seIndex>1 && rowNum>1){
                        ParseNxParamsTransferVo paramsTransferVo = new ParseNxParamsTransferVo();
                        paramsTransferVo.setNxParamName(sheetName);
                        paramsTransferVo.setToolManageParamName(data.getType());
                        paramsTransferVo.setSheetNo(seIndex);
                        list.add(paramsTransferVo);
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).doReadAll();
            if (list.size() > 0) {
                assemblyToolService.importNxParamsTransferExcelReload(list);
            }
            //return ResponseBody.success(assemblyToolService.GetAssemblyTaskStatus(assemblyToolId));
            return ResponseBody.success();
        }  catch (IOException e) {
            return ResponseBody.error("1001" , "图片上传失败");
        }
    }

//    @RequestMapping(value = "/checkAssemblyExist", method = RequestMethod.GET)
//    public ResponseBody checkAssemblyExist(@RequestParam Integer assemblyToolId) {
//        try {
//            return ResponseBody.success(assemblyToolService.checkAssemblyExist(assemblyToolId));
//        } catch (ToolManageException e) {
//            return ResponseBody.error(e.getCode() , e.getMessage());
//        }
//    }

    @RequestMapping(value = "/GetNxProgramParams", method = RequestMethod.POST)
    public ResponseBody GetNxProgramParams(@RequestBody NxProgramParamsReq nxProgramParamsReq) {
            if(ObjectUtil.isNotNull(nxProgramParamsReq)){
                String nxProgramCode = "";
                if(ObjectUtil.isNotNull(nxProgramParamsReq.getNxProgram()) && StringUtils.isNotBlank(nxProgramParamsReq.getNxProgram().getProgramName())){
                    NxProgramEntity nxProgramEntity = new NxProgramEntity();
                    nxProgramCode = "nxProgram" + UUIDUtil.getUUID();
                    nxProgramEntity.setNxProgramCode(nxProgramCode);
                    nxProgramEntity.setName(nxProgramParamsReq.getNxProgram().getProgramName());
                    try{
                        nxProgramEntity.setTotalTime(Double.valueOf(nxProgramParamsReq.getNxProgram().getTotalTime()));
                    }catch (Exception e){

                    }
                    nxProgramMapper.insert(nxProgramEntity);
                }
                List<NxSubprogramEntity> insertList = new ArrayList<>();
                if(CollectionUtil.isNotEmpty(nxProgramParamsReq.getNxSubprograms())){
                    for (NxSubprogram nxSubprogram : nxProgramParamsReq.getNxSubprograms()) {
                        if(ObjectUtil.isNull(nxSubprogram) || StringUtils.isBlank(nxSubprogram.getSubProgramName())){
                            continue;
                        }
                        NxSubprogramEntity nxSubprogramEntity = new NxSubprogramEntity();
                        BeanUtils.copyProperties(nxSubprogram , nxSubprogramEntity);
                        if(ObjectUtil.isNotNull(nxSubprogram.getNxToolInfo())){
                            nxSubprogramEntity.setToolNo(nxSubprogram.getNxToolInfo().getToolName());
                        }
                        nxSubprogramEntity.setNxProgramCode(nxProgramCode);
                        nxSubprogramEntity.setNxSubProgramCode("nxSubprogramCode" + UUIDUtil.getUUID());
                        insertList.add(nxSubprogramEntity);
                    }
                    if(CollectionUtil.isNotEmpty(insertList)){
                        nxSubprogramMapper.insertBatchSomeColumn(insertList);
                    }
                }
            }
            return ResponseBody.success();
    }
}
